//
//  JLSortViewController.m
//  Liang美团HD
//
//  Created by sun on 16/1/20.
//  Copyright © 2016年 sun. All rights reserved.
//

#import "JLSortViewController.h"
#import "JLData.h"
#import "JLSort.h"
@interface JLSortViewController ()
@property(nonatomic,strong)NSArray * sortArray;
@end

@implementation JLSortViewController
-(NSArray *)sortArray{
    if (_sortArray == nil) {
        _sortArray = [JLData sortArray];
    }
    return _sortArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat margin = 10;
    CGFloat x = margin;
    CGFloat width = 100;
    CGFloat height = 30;
    UIButton * lastBtn = nil;
    for (NSUInteger i = 0; i < self.sortArray.count; i++) {
        UIButton * btn = [[UIButton alloc]init];
       
        JLSort * sort = self.sortArray[i];
        [btn setTitle:sort.label forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        CGFloat y = margin + i * (margin + height);
        btn.frame = CGRectMake(x, y, width, height);
        btn.tag = i ;
        [btn setBackgroundImage:[UIImage imageNamed:@"btn_filter_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"btn_filter_selected"] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(sortButtonClick:) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:btn];
        lastBtn = btn;
    }
    CGFloat maxW = CGRectGetMaxX(lastBtn.frame) + margin;
    CGFloat maxH = CGRectGetMaxY(lastBtn.frame) + margin;
    self.preferredContentSize = CGSizeMake(maxW, maxH) ;
    
}
-(void)sortButtonClick:(UIButton *)btn{
    [[NSNotificationCenter defaultCenter] postNotificationName:JLSortButtonDidClickNotification object:self.sortArray[btn.tag]];
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
